package com.youlu.campus.admin.community.controller;

import com.youlu.campus.admin.annotation.AuthParamTypeEnum;
import com.youlu.campus.admin.annotation.RequestActivityAuth;
import com.youlu.campus.admin.utils.AuthUtils;
import com.youlu.campus.entity.ActivityTaskInfo;
import com.youlu.campus.entity.VO.req.ActivityTaskQueryVO;
import com.youlu.campus.service.activity.ActivityTaskService;
import com.yuelin.infrastructure.quantum.common.QResult;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.NotNull;

@Slf4j
@RestController
public class ActivityTaskController {
    @Autowired
    private ActivityTaskService activityTaskService;

    @PostMapping("/activity/task/create")
    @RequestActivityAuth(activityId = "#request.activityId",message = "无权限创建任务")
    public QResult createActivityTask(@RequestBody @NotNull ActivityTaskInfo request) {
        request.setCreatorId(AuthUtils.getUserId());
        boolean result = activityTaskService.create(request);
        return QResult.success(result);
    }

    @PostMapping("/activity/task/update")
    //@RequestActivityAuth(activityId = "#request.activityId",message = "无权限修改任务")
    public QResult updateActivityTask(@RequestBody ActivityTaskInfo request) {
        boolean result = activityTaskService.update(request);
        return QResult.success(result);
    }

    @GetMapping("/activity/task/delete")
    @RequestActivityAuth(activityId = "#id",paramType = AuthParamTypeEnum.TASK,message = "无权限删除任务")
    public QResult deleteActivityTask(@RequestParam("id") String id) {
        boolean result = activityTaskService.delete(id);
        return QResult.success(result);
    }

    @PostMapping("/activity/task/list")
    public QResult activityListTask(@RequestBody ActivityTaskQueryVO request) {
        try {
            return QResult.success(activityTaskService.list(request));
        } catch (Exception e) {
            log.error(":>>> 活动任务列表错误:{}", e);
            return QResult.fail("活动任务列表错误");
        }

    }

    @GetMapping("/activity/task/find-one")
    public QResult getActivityTask(@RequestParam("id") String id) {
        return QResult.success(activityTaskService.findOne(id));
    }
}
